﻿
// actionFlag: 1 -> 新增；2 -> 修改；3 -> 查看
var actionFlag = getPar1("actionFlag");

$(document).ready(function () {
    $("#BankAccount").numberbox({
        min: 0,
        precision: 0
    });

    $("#Credit").numberbox({
        min: 1,
        precision: 0
    });

    $("#CreditPeriod").numberbox({
        min: 1,
        precision: 0
    });

    if (actionFlag == 1) {
        obj = $("#CompanyNo");
        genNewSequenceNo(obj, "CompanyNo");
        $("#ShortName").focus();
    }

    if (actionFlag == 2) {
        parent.setAddNewButtonHiden();
    }

    $("#EMail").bind("change", function () { var email = $(this).val(); isEmail(email); });
    $("#Mobile").bind("change", function () { var mobile = $(this).val(); isMobile(mobile); });
    $("#Tel").bind("change", function () { var tel = $(this).val(); isPhone($("#Tel"), tel, "联系电话不合法!"); });
    $("#Fax").bind("change", function () { var fax = $(this).val(); isPhone($("#Fax"), fax, "传真号码不合法!"); });
    $("#ShortName").bind("change", function () { var shortName = $(this).val(); isExistsShortName(shortName); });
    $("#CompanyName").bind("change", function () { var companyName = $(this).val(); isExistsCompanyName(companyName); });
});

var customSetEvent = function () {
    if (actionFlag == "2") {
        //parent.setAddNewButtonHiden();
        $("#Credit").val(parseFloat($("#Credit").val().toString()).toFixed(0).toString());
    }
}

// 检测厂家简称是否重复
function isExistsShortName(val) {
    if (val != "") {
        var shortName = fnGetData("BH_Company", "CompanyID<>'" + instance[0].CompanyID.toString() + "' and ShortName='" + val + "'");
        if (shortName != null && shortName != "") {
            alert("厂家简称: [ " + val + " ] 已存在, 请重新输入!");
            setTimeout(function () {
                $("#ShortName").focus();
                $("#ShortName").val("");
            }, 10);
        }
    }
}

// 检测厂家名称是否重复
function isExistsCompanyName(val) {
    if (val != "") {
        var companyName = fnGetData("BH_Company", "CompanyID<>'" + instance[0].CompanyID.toString() + "' and CompanyName='" + val + "'");
        if (companyName != null && companyName != "") {
            alert("厂家简称: [ " + val + " ] 已存在, 请重新输入!");
            setTimeout(function () {
                $("#CompanyName").focus();
                $("#CompanyName").val("");
            }, 10);
        }
    }
}

// 检测联系电话或传真
function isPhone(o, val, msg) {
    if (val != "") {
        var myreg = /^0\d{2,3}-?\d{7,8}$/;
        if (!myreg.test(val)) {
            alert(msg);
            setTimeout(function () {
                o.focus();
                o.val("");
            }, 10);
        }
    }
}

// 检测电子邮箱格式
function isEmail(val) {
    if (val != "") {
        var myreg = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;
        if (!myreg.test(val)) {
            alert("邮箱格式不合法!");
            setTimeout(function () {
                $("#EMail").focus();
                $("#EMail").val("");
            }, 10);
            return;
        }
    }
}

// 检测手机号格式
function isMobile(val) {
    if (val != "") {
        var myreg = /^(?:13\d|14\d|15\d|18\d)-?\d{5}(\d{3}|\*{3})$/;
        if (!myreg.test(val)) {
            alert("手机号格式不正确!");
            setTimeout(function () {
                $("#Mobile").focus();
                $("#Mobile").val("");
            }, 10);
        }
    }
}

// 提交检测
var fnCustomCheck = function () {
    if (instance[0].CompanyName == "" || instance[0].CompanyName == null) {
        //alert("厂家名称不能为空!");
        $('#CompanyName').focus();
        return "厂家名称不能为空!";
    }
    if (instance[0].CompanyType == "" || instance[0].CompanyType == null) {
        //alert("厂家类型不能为空!");
        $('#CompanyType').focus();
        return "公司类型不能为空!";
    }
    if (instance[0].CompanyFlag == "" || instance[0].CompanyFlag == null) {
        //alert("厂家标识不能为空!");
        $('#CompanyFlag').focus();
        return "公司标识不能为空!";
    }

    return "";
}

var fnSetValue = function () {
    instance[0].CompanyName = instance[0].CompanyName.toUpperCase();
    instance[0].ShortName = instance[0].ShortName.toUpperCase();
    instance[0].Bank = instance[0].Bank.toUpperCase();
    instance[0].TaxAccount = instance[0].TaxAccount.toUpperCase();
    instance[0].Region = instance[0].Region.toUpperCase();
}

function RefreshDataParent() {
    if (parent.NeedRefreshPage != null) {
        if (typeof (parent.NeedRefreshPage.LoadData) == "function") {
            parent.NeedRefreshPage.RefreshData();
        }
    }
}  